package userInterface.Panels;

import dataAccess.*;
import settings.*;

import java.awt.event.*;
import javax.swing.*;

/**
 * UI JPanel for editing the Settings.
 * @author BDS
 */
public class PanelSettings extends PanelBase
{
	private static final long serialVersionUID = 1L;
	
	private Settings settings;

	private ActionListener settingsActionListener;
	
	private SpringLayout slSettingsLayout;
	
	private JButton btnSave;
	private JButton btnRevert;

	/**
	 * Class constructor.
	 */
	public PanelSettings()
	{
		settingsActionListener = new SettingsActionListener();
		
		slSettingsLayout = new SpringLayout();
		setLayout(slSettingsLayout);

		btnSave = new JButton("Save");
		btnSave.setMnemonic(KeyEvent.VK_S);
		btnSave.addActionListener(settingsActionListener);
		slSettingsLayout.putConstraint(SpringLayout.SOUTH, btnSave, -10, SpringLayout.SOUTH, this);
		add(btnSave);
		
		btnRevert = new JButton("Revert");
		btnRevert.setMnemonic(KeyEvent.VK_R);
		btnRevert.addActionListener(settingsActionListener);
		slSettingsLayout.putConstraint(SpringLayout.EAST, btnSave, -6, SpringLayout.WEST, btnRevert);
		slSettingsLayout.putConstraint(SpringLayout.EAST, btnRevert, -10, SpringLayout.EAST, this);
		slSettingsLayout.putConstraint(SpringLayout.SOUTH, btnRevert, -10, SpringLayout.SOUTH, this);
		add(btnRevert);
		
		setEditedState(false);
	}
	
	/**
	 * Prompts the user to save any relevant changes, if necessary.
	 */
	@Override
	public void promptSaveChanges()
	{
		if (settings != null && btnSave.isEnabled())
		{
			if (JOptionPane.showConfirmDialog(null,
											  "Save all settings changes?",
											  "Settings",
											  JOptionPane.YES_NO_OPTION,
											  JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION)
			{
				save();
			}
		}
	}
	
	/**
	 * Saves the Settings.
	 */
	public void save()
	{
		setEditedState(false);
		LocalDataAccess.saveSettings(settings);
		resetFocus();
	}
	
	/**
	 * Loads the Settings.
	 */
	@Override
	public void load()
	{
		setEditedState(false);
		settings = LocalDataAccess.loadSettings();
		
		updateSettings();
		resetFocus();
	}
	
	/**
	 * Sets the availability of the Save and Revert buttons based on the given state.
	 * @param edited True if any Settings have been changed; false if they reflect the settings.xml file.
	 */
	private void setEditedState(boolean edited)
	{
		btnSave.setEnabled(edited);
		btnRevert.setEnabled(edited);
	}
	
	/**
	 * Updates the Settings to reflect what the UI currently displays.
	 */
	private void updateSettings()
	{
		validate();
	}
	
	/**
	 * ActionListener for the Settings JButton objects.
	 * @author BDS
	 */
	private class SettingsActionListener implements ActionListener
	{
		/**
		 * Executes specific commands based on the ActionEvent.
		 * Save - Saves all Settings changes.
		 * Revert - Reverts all Settings changes to their previously saved sate.
		 * Default - Update the Settings.
		 */
		@Override
		public void actionPerformed(ActionEvent actionEvent)
		{
			if (actionEvent.getActionCommand().equals(btnSave.getActionCommand()))
			{
				save();
			}
			else if (actionEvent.getActionCommand().equals(btnRevert.getActionCommand()))
			{
				load();
			}
			else
			{
				updateSettings();
			}
		}
	}
}
